<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../res/css/common/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../res/layui-config/css/app.css" media="all">
		<title>质检明细</title>
    </script>
	</head>
	
	
	
	<script type="text/html" id="input">
	<div class="layui-inline">
		<input class="layui-input" maxlength="5" style="border:none;padding-bottom:8px;" name="qcFail" value="0"  autocomplete="off" id="number{{d.qdetId}}" onkeyup="Pressdown(this)">
	</div>
	</script>
	
	<script type="text/html" id="inputss">
	<div class="layui-inline">
		<input class="layui-input layui-disabled"  readonly="readonly" name="qdetRate" value="0"  maxlength="6" style="border:none;padding-bottom:8px;" id="price{{d.qdetId}}" autocomplete="off">
	</div>
	</script>
	<script type="text/html" id="inputsss">
	 
		<input class="layui-input layui-disabled" id="number_{{d.qdetId}}"  readonly="readonly"  value="0"  maxlength="6" style="border:none;padding-bottom:8px;" autocomplete="off">
	 
	</script>
	<script type="text/html" id="inputssss">
	 
		<input class="layui-input layui-disabled" id="sumNumber_{{d.qdetId}}"  readonly="readonly"  value="{{d.qdetAmount}}"  maxlength="6" style="border:none;padding-bottom:8px;" autocomplete="off">
	 
	</script>
	
	<body>
		<div>
			<input type="hidden" id="shuju" style="width: 100%">
		</div>
		<div class="kit-table">
			<!-- 左上角的两个按钮 -->
		        <!-- 左上角的两个按钮 -->
		        <div class="kit-table-header">
		        
					<div class="layui-form-item">
					   <label class="layui-form-label"></label>
					   <div class="layui-input-inline">
					     <input type="hidden" name="qcFail" value="0" id="qcFail" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
					   </div>
					   <label class="layui-form-label"></label>
					   <div class="layui-input-inline">
					     <input type="hidden" name="qcRate" value="100" id="qcRate" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
					   </div>
					   <input type="hidden" name="qcAmount" value="0" id="qcAmount" autocomplete="off" class="layui-input layui-disabled" readonly="readonly">
					</div>
					
				
		            <div class="kit-search-btns" style="width: 100px;text-align:right;margin-top: -30px;">
		             <a class="layui-btn layui-btn-sm" id="add"><i class="layui-icon"></i> 提交</a>
		        	</div>
		        </div>
			
			<!-- 操作按钮 -->
		    <div class="kit-table-body">
		        <table id="demo" lay-filter="demo"></table>
		        <script type="text/html" id="barDemo">
		            <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
		            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
		            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		        </script>
		    </div>
		</div>
		
		
		<script src="../../res/js/common/jquery-1.11.2.min.js"></script>
		<script>
			//文本框只能输入金额
		    function amount(th){
		        var regStrs = [
		            ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0
		            ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点
		            ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点
		            ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上
		        ];
		        for(i=0; i<regStrs.length; i++){
		            var reg = new RegExp(regStrs[i][0]);
		            th.value = th.value.replace(reg, regStrs[i][1]);
		        }
		    }
			//搜索框同步输入
			function lockInput(obj,id){
				$("#"+id).val($(obj).val());
			}
		</script>
		<script src="../../res/layui/layui.all.js"></script>
		<script src="../../res/js/common/parameter.js"></script>
		<script>
			window.windata=null;
			var tableElem;
			//显示表格数据
		    layui.use(['table','upload'], function() {
		    	var qcId = getParameter("id");
		    	
		        var table = layui.table,
		            $ = layui.jquery,
		            layer = layui.layer,
		            form = layui.form,
		            laytpl = layui.laytpl;
		        var tableIns = table.render({
		            elem: '#demo',
		            height: 'full-50', //容器高度
		            url: '../../qcDetail/findBuyId.action?qcId='+qcId,
		            page: true,
		            id: 'demo',
		            cols: [
		                [ 
		                {field: 'qdetId',title: 'ID',width: 120},	
		                {field: 'matName',title: '材料名',width: 120},
		                {field: 'qdetFail',title: '未通过数量',width: 120,templet:'#input'},
		                {field: 'qdetAmount',title: '质检数量',width: 120},
		                {field: 'qdetAmounts',width: 120,templet:'#inputsss'},
		                {field: 'qdetAmountss',width: 120,templet:'#inputssss'},
		                {field: 'qdetRate',title: '质检概率',width: 150,templet:'#inputss'}]
		            ],
		            done: function(res, curr, count) {
		            	
		                $(".layui-table-box").find("[data-field='qdetAmounts']").css("display","none");
		                $(".layui-table-box").find("[data-field='qdetAmountss']").css("display","none");
		            	
		            	windata=res.data;
		            	tableElem = this.elem.next('.layui-table-view');
		            	var tables=table.cache.demo;
		            	
		            	$.each(tables,function(i,item){
				        	$("#shuju").val($("#shuju").val()+item.qdetId+","+item.matName+","+$("#number"+item.qdetId).val()+","+item.qdetAmount+","+$("#price"+item.qdetId).val()+"#");	
		            	})
		            	
		            },
		            loading: true
		        });
		        
				
		        
		        
		        
		        //监听搜索表单提交
		        form.on('submit(search)', function(data) {  
		            //带条件查询
		            tableIns.reload({
		                where: data.field,
		                page: { curr: 1 }
		            });
		            $('.kit-search-mored').hide();
		            return false;
		        });
		        
		        
		        $("#add").click(function(){
		        	//加载层
		        	var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
		        	//loading层
		        	var index = layer.load(1, {
		        	  shade: [0.1,'#fff'] //0.1透明度的白色背景
		        	});
		        	
   	        		var url="../../qc/Qcadd.action";
   					var data={"id":qcId,"shuju":$("#shuju").val(),"sumAmout":$("#qcFail").val(),"sumRate":$("#qcRate").val(),"zjAmout":$("#qcAmount").val(),"qcState":1}
   					
					$.post(url,data,function(mes){
						if(mes.status==1){
							location.href="Qcbuy.html";
						}
					})
   					
   				})
		        
		        
		        //表单渲染
		        form.render(null, 'kit-search-form');
		        //更多筛选点击事件
		        $('#kit-search-more').on('click', function() {
		            $('.kit-search-mored').toggle();
		        });
		        var editIndex;
		    });
			
			
		  	//窗口自适应
			function autoHeight() {
				var h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
				//监听改变窗口大小
				window.onresize = function() {
					h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
					tableIns.reload({
						height: h
					});
				}
				return h;
			}
			
			
		  	//全屏弹窗
			function maxlayer(){
				var index = layer.open({
				  type: 2,
				  title: '药品配置',
				  content: 'https://www.baidu.com',
				  area: ['800px', '430px']
				  //maxmin: true
				});
				layer.full(index);
			}
		  	
		  	//重新渲染表单
		  	function renderForm(){ 
	  			layui.use('form', function(){ 
		  			var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加() 
		  			form.render(); 
	  			}); 
	  		}
		    
		</script>
		<script>
			$(function(){
				$("#fh").click(function(){
					location.href="purchase.html";
				})
			})
		</script>
		
		
		<script>
			
			function Pressdown(obj){
				obj.value = obj.value.replace(/[^\d]/g,"");//清除“数字”和“.”以外的字符  
			    if(obj.value ==0){
			    	obj.value = "0";
			    }
			    if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
			        obj.value= parseFloat(obj.value); 
			    }
			    if(obj.value.indexOf(".")==0){//第一个字符不能为.
			    	 obj.value="0";
			    }
			    
			    
			    var id = obj.id;
			    
				var a = obj.value;//数量值
				var b=$(obj).parent().parent().parent().next().children().html();//获取质检数量值
				
				
				
				if(parseInt(b)-parseInt(a)<0){
					var c=100;
				}else{
					//(质检数量-质检未通过数)/质检数量*100
					var c=(parseInt(b)-parseInt(a))/parseInt(b)*100;
					//var c=(parseInt(b)+parseInt(a))/parseInt(a);
				}
				
				//var c =parseFloat(a)/parseFloat(b);//用this*数量
				//截取后面两位小数
				var d=c.toFixed(1);
				
				//计算结果显示
				$(obj).parent().parent().parent().next().next().next().next().children().children().children().val(d+'%');
				
				/*var sss=parseInt(b)-parseInt(a);
				$(obj).parent().parent().parent().next().children().text(sss);*/
			
				
				if(parseInt($(obj).val())>parseInt($("#number_"+id.split("number")[1]).val())){
					$(obj).parent().parent().parent().next().children().text(parseInt($("#sumNumber_"+id.split("number")[1]).val())-a);
				}else{
					$(obj).parent().parent().parent().next().children().text(parseInt(b)+(parseInt($("#number_"+id.split("number")[1]).val())-a));
				}
				
				$("#number_"+id.split("number")[1]).val($(obj).val())
				
				//另外写的质检数量
				var kk=$(obj).parent().parent().parent().next().next().next().children().children().val();
				//判断如果输入的值大于质检数量的值的话
				if(parseInt(a)>parseInt(kk)){
					//就把输入的值等于0
					obj.value = "0";
					//把另外一个质检数量赋进来
					$(obj).parent().parent().parent().next().children().html(kk);
					
					layer.msg("不能大于质检数量噢");
				}else{
					$(obj).parent().parent().parent().next().children().html(kk-a);
				}
				
				//质检总未通过数
				var amo=allAmount();
				$("#qcFail").val(amo);
				
				//质检总数量
				var zj=zjAmount();
				$("#qcAmount").val(zj);
				
				//质检总通过率
				var zongde=(zj-amo)/zj*100;
				console.log(zj);
				console.log(amo);
				var bit=zongde.toFixed(1);
				$("#qcRate").val(bit);
				
				//改变上面的动态值
				$("#shuju").val("");
				 var tbody=$(obj).parent().parent().parent().parent().parent();
				 $.each($(tbody).find("tr"),function(i,item){
					 
					var bdetFkId=$(item).children().get(0);
					var matName=$(item).children().get(1);
					var zjNumber=$(item).children().get(3); 
					
					//未通过数
					//var shuru=$("#number"+$(bdetFkId).children().text()).val();
					//console.log(shuru);
					//质检数量
					//var shuliang=$(zjNumber).children().text();
					//console.log(shuliang);
					//减完后的值
					//var sjNumber=shuliang-shuru;
					
					 
					$("#shuju").val($("#shuju").val()+$(bdetFkId).children().text()+","+$(matName).text()+","+$("#number"+$(bdetFkId).children().text()).val()+","+$(zjNumber).children().text()+","+$("#price"+$(bdetFkId).children().text()).val()+"#");	
				 })
				
				
			}
			
			
			
			
			//未通过数量相加
			function allAmount(){
				var amount = 0;
				$.each(tableElem.find('td[data-field="qdetFail"]').find('input'),function(index,item){
					amount += parseInt($(this).val());
				});
				return amount;
			}
			
			//质检数量相加
			function zjAmount(){
				var zjamount = 0;
				$.each(tableElem.find('td[data-field="qdetAmount"]'),function(index,item){
					zjamount += parseInt($(this).text());
 				});
				return zjamount;
			}
		  	
		  	//质检总概率相加
		  	function allprobability(){
		  		var probability=0;
		  		$.each(tableElem.find('td[data-field="qdetRate"]').find('input'),function(index,item){
		  			probability += parseFloat($(this).val());
				});
		  		return probability;
		  	}
		</script>
		
	</body>
</html>
